package net.hvacapp.cmvrfforandroid.ui.project;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mirror.android.common.util.StringUtils;

import net.hvacapp.cmvrfforandroid.R;
import net.hvacapp.cmvrfforandroid.http.BasicHttpClient;
import net.hvacapp.cmvrfforandroid.http.HttpCallbackHandle;
import net.hvacapp.cmvrfforandroid.http.HttpConstant;
import net.hvacapp.cmvrfforandroid.manager.AccountManager;
import net.hvacapp.cmvrfforandroid.manager.ProjectManager;
import net.hvacapp.cmvrfforandroid.ui.BaseActivity;
import net.hvacapp.cmvrfforandroid.ui.project.model.FactorData;
import net.hvacapp.cmvrfforandroid.ui.project.model.GetFactorDataResp;
import net.hvacapp.cmvrfforandroid.ui.project.model.GetIndoorEquipDataResp;
import net.hvacapp.cmvrfforandroid.ui.project.model.GetProductDataResp;
import net.hvacapp.cmvrfforandroid.ui.project.model.GetRoomDataResp;
import net.hvacapp.cmvrfforandroid.ui.project.model.GuidData;
import net.hvacapp.cmvrfforandroid.ui.project.model.IndoorEquipData;
import net.hvacapp.cmvrfforandroid.ui.project.model.ProjectInfoData;
import net.hvacapp.cmvrfforandroid.ui.project.model.RoomData;
import net.hvacapp.cmvrfforandroid.ui.project.view.DeviceItemView;
import net.hvacapp.cmvrfforandroid.utils.AnnotationUtil;
import net.hvacapp.cmvrfforandroid.utils.NumberUtil;
import net.hvacapp.cmvrfforandroid.widget.CellSeperateLineView;
import net.hvacapp.cmvrfforandroid.widget.IOSAlertView;
import net.hvacapp.cmvrfforandroid.widget.UserSelectionView;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@EActivity(R.layout.activity_room_info)
public class RoomInfoActivity extends BaseActivity {

    private final int Select_SNJ = 1;
    private final int Select_QXSYKT = 2;
    private final int Select_SNJ_XS = 3;
    private final int Select_QXSYKT_XS = 4;

    private Dialog mProgressDlg;

    @ViewById(R.id.tv_room_name)
    TextView mRoomName;

    @ViewById(R.id.cet_area)
    EditText mAreaCet;
    @ViewById(R.id.cet_load)
    EditText mLoadCet;

    @ViewById(R.id.sel_snj)
    UserSelectionView mSnjSel;
    @ViewById(R.id.ll_snj_content)
    LinearLayout mSnjContentLl;
    @ViewById(R.id.tv_snj)
    TextView mSnjTv;

    @ViewById(R.id.sel_qxsykt)
    UserSelectionView mQxsyktSel;
    @ViewById(R.id.ll_qxsykt_content)
    LinearLayout mQxsyktContentLl;
    @ViewById(R.id.tv_qxsykt)
    TextView mQxsyktTv;

    @ViewById(R.id.ll_snj)
    LinearLayout mSnjLl;
    @ViewById(R.id.ll_qxsykt)
    LinearLayout mQxsyktLl;

    private RoomData mRoom;
    private ArrayList<RoomData> mRooms; //所有的房间
    private int mCurrentIndex; //当前编辑的房间id
    private FactorData mFactorData;

    private ArrayList<IndoorEquipData> mSnjSelectedDevice; //房间中绑定了的室内机
    private ArrayList<IndoorEquipData> mQxsyktSelectedDevice;

    private ArrayList<GuidData> mSnjDevice; //从列表中选择过来的室内机
    private ArrayList<GuidData> mQxsyktDevice;

    private int loadFinishedCount;
    private int loadTotalCounts;



    /**
     * 页面加载完后执行
     */
    @AfterViews
    @Override
    protected void viewDidLoad() {
        super.viewDidLoad();
        initViews();
        initEvent();
    }

    /***
     * 初始化视图
     */
    private void initViews() {
        if (ProjectManager.getInstance().snjDeviceEnabled()) {
            mSnjLl.setVisibility(View.VISIBLE);
            mSnjSel.setTitleString("室内机");
            mSnjSel.setInfoString("选择机型");
        }
        else {
            mSnjLl.setVisibility(View.GONE);
        }

        if (ProjectManager.getInstance().qxsyktDeviceEnabled()) {
            mQxsyktLl.setVisibility(View.VISIBLE);
            mQxsyktSel.setTitleString("轻型商用空调");
            mQxsyktSel.setInfoString("选择机型");
        }
        else {
            mQxsyktLl.setVisibility(View.GONE);
        }



    }

    /**
     * 初始化
     */
    @Override
    public void initBoot() {
        setupToolbar(0, "房间信息", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putParcelable("room", mRoom);
                intent.putExtras(bundle);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }


    /**
     * 初始化数据
     */
    @Override
    public void initData(Intent intent) {
        mSnjSelectedDevice = new ArrayList<>();
        mQxsyktSelectedDevice = new ArrayList<>();

        mSnjDevice = new ArrayList<>();
        mQxsyktDevice = new ArrayList<>();
        mRooms = intent.getParcelableArrayListExtra("rooms");
        mCurrentIndex = intent.getIntExtra("index", 0);

        if (mCurrentIndex < mRooms.size()) {
            setButton1Info("下一个", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mCurrentIndex < mRooms.size() - 2) {
                        mCurrentIndex++;
                        loadEditRoomData();
                    }
                    else if(mCurrentIndex == mRooms.size() - 2){
                        //隐藏按钮
                        mCurrentIndex++;
                        loadEditRoomData();
                        setButton1Info(null, null);
                    }
                    else {
                        setButton1Info(null, null);
                    }
                }
            });
        }

        loadEditRoomData();


    }

    private void loadEditRoomData() {

        if (mCurrentIndex < mRooms.size()) {
            mRoom = mRooms.get(mCurrentIndex);
        }
        else {
            return;
        }

        loadFinishedCount = 0;
        loadTotalCounts = 1;
        mProgressDlg = createProgressBarDialog(this, "数据加载中...");
        mProgressDlg.show();
        if (mRoom != null) {
            mRoomName.setText("房间名称：" + mRoom.getName());
            if (mRoom.getArea() > 0) {
                mAreaCet.setText(NumberUtil.formateDouble(mRoom.getArea()));
            }
            else {
                mAreaCet.setText("");
            }
            if (mRoom.getLoad() > 0) {
                mLoadCet.setText(NumberUtil.formateDouble(mRoom.getLoad()));
            }
            else {
                mLoadCet.setText("");
            }
            loadFactorData();
            if (ProjectManager.getInstance().snjDeviceEnabled()) {
                loadTotalCounts++;
                loadRoomDevice(ProjectInfoData.MACHINE_TAG_FOR_SNJ);
            }
            if (ProjectManager.getInstance().qxsyktDeviceEnabled()) {
                loadTotalCounts++;
                loadRoomDevice(ProjectInfoData.MACHINE_TAG_FOR_QXSYKT);
            }
        }

        loadSelectedSNJSeries();
    }



    /**
     * 页面名称
     */
    @Override
    public String getName() {
        return "";
    }

    private void initEvent() {
        mSnjSel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(RoomInfoActivity.this, AnnotationUtil.get(SelectDeviceXsActivity.class));
                Bundle bundle = new Bundle();
//                bundle.putParcelableArrayList("device", mSnjDevice);
                bundle.putBoolean("multiple", false);
                bundle.putString("tag", ProjectInfoData.MACHINE_TAG_FOR_SNJ);
                intent.putExtras(bundle);
                startActivityForResult(intent, Select_SNJ);
            }
        });

        mQxsyktSel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(RoomInfoActivity.this, AnnotationUtil.get(SelectDeviceXsActivity.class));
                Bundle bundle = new Bundle();
//                bundle.putParcelableArrayList("device", mQxsyktDevice);
                bundle.putBoolean("multiple", false);
                bundle.putString("tag", ProjectInfoData.MACHINE_TAG_FOR_QXSYKT);
                intent.putExtras(bundle);
                startActivityForResult(intent, Select_QXSYKT);
            }
        });

        mSnjTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(RoomInfoActivity.this, AnnotationUtil.get(SelectXsActivity.class));
                Bundle bundle = new Bundle();
                bundle.putBoolean("multiple", false);
                bundle.putString("tag", ProjectInfoData.MACHINE_TAG_FOR_SNJ);
                intent.putExtras(bundle);
                startActivityForResult(intent, Select_SNJ_XS);
            }
        });

        mQxsyktTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(RoomInfoActivity.this, AnnotationUtil.get(SelectXsActivity.class));
                Bundle bundle = new Bundle();
                bundle.putBoolean("multiple", false);
                bundle.putString("tag", ProjectInfoData.MACHINE_TAG_FOR_QXSYKT);
                intent.putExtras(bundle);
                startActivityForResult(intent, Select_QXSYKT_XS);
            }
        });

        mAreaCet.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Double area = 0.0;
                try {
                    //if ((heighText.getText().toString())!=null)
                    area = Double.parseDouble(s.toString());
                    Double load = area * mFactorData.getColdLoadFactor();
                    mLoadCet.setText(load.toString());

                } catch (Exception e) {
                    showMessage("请输入数字");
                }

                if (mAreaCet.getText() != null && !StringUtils.isEmpty(mAreaCet.getText().toString())) {
                    mRoom.setArea(Double.valueOf(mAreaCet.getText().toString().trim()));
                }
                if (mLoadCet.getText() != null && !StringUtils.isEmpty(mLoadCet.getText().toString())) {
                    mRoom.setLoad(Double.valueOf(mLoadCet.getText().toString().trim()));
                }

                setRoomData();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    /**
     * 获取负载参数
     */
    private void loadFactorData() {
        Map<String, Object> param = new HashMap<>();
        param.put("cid", ProjectManager.getInstance().getProjectData().getCityId());
        param.put("rn", mRoom.getName());

        BasicHttpClient.httpGetAsync(HttpConstant.HTTP_GET_FACTOR_DATA, AccountManager.getInstance().getToken(), param, new HttpCallbackHandle<GetFactorDataResp>() {
            @Override
            public void onSuccess(GetFactorDataResp obj) {
                // 成功
                loadFinishedCount++;
                if (loadFinishedCount == loadTotalCounts) {
                    mProgressDlg.dismiss();
                }
                mFactorData = obj.getGetLoadFactorByRoomNameResult();
            }

            @Override
            public void onFailure(String response) {
                loadFinishedCount++;
                if (loadFinishedCount == loadTotalCounts) {
                    mProgressDlg.dismiss();
                }
                showMessage("获取负荷参数失败，请重试！");
            }
        });
    }



    /**
     *  获取项目中选择了的室内机系列，用来设置默认的选择型式，以便自动选型
     */
    private void loadSelectedSNJSeries() {
        Map<String, Object> param = new HashMap<>();
        param.put("pid", ProjectManager.getInstance().getProjectData().getGuidId());
        param.put("tag", ProjectInfoData.MACHINE_TAG_FOR_SNJ);

        BasicHttpClient.httpGetAsync(HttpConstant.HTTP_GET_PROJECT_PRODUCT_SERIES, AccountManager.getInstance().getToken(), param, new HttpCallbackHandle<GetProductDataResp>() {
            @Override
            public void onSuccess(GetProductDataResp obj) {
                // 成功
                if (obj.getGetCandidateXlsResult().size() > 0) {
                    loadSelectedXs(obj.getGetCandidateXlsResult().get(0).getGuidId(), ProjectInfoData.MACHINE_TAG_FOR_SNJ);
                }
            }

            @Override
            public void onFailure(String response) {
                showMessage("获取室内机系列失败，请重试！");
            }
        });
    }

    /**
     *  获取项目中选择了的型式列表(室内机和轻型商用空调)
     *
     *  @param series 系列
     *  @param tag   系列数据
     */
    private void loadSelectedXs(String series, final String tag) {
        Map<String, Object> param = new HashMap<>();
        param.put("xl", series);
        param.put("tag", ProjectInfoData.MACHINE_TAG_FOR_SNJ);
        param.put("ac", 1); //0 仅包括未上市  1 仅包括已上市 2 已上市未上市都包括

        BasicHttpClient.httpGetAsync(HttpConstant.HTTP_GET_PROJECT_PRODUCT_XS_BY_XL, AccountManager.getInstance().getToken(), param, new HttpCallbackHandle<GetProductDataResp>() {
            @Override
            public void onSuccess(GetProductDataResp obj) {
                // 成功
                if (obj.getGetProductXssResult().size() > 0) {
                    if (StringUtils.isEquals(tag, ProjectInfoData.MACHINE_TAG_FOR_SNJ)) {
                        ProjectManager.getInstance().setProjectDefaultSeries(ProjectInfoData.MACHINE_TAG_FOR_SNJ, obj.getGetProductXssResult().get(0).getGuidId());
                    } else if (StringUtils.isEquals(tag, ProjectInfoData.MACHINE_TAG_FOR_QXSYKT)) {
                        ProjectManager.getInstance().setProjectDefaultSeries(ProjectInfoData.MACHINE_TAG_FOR_QXSYKT, obj.getGetProductXssResult().get(0).getGuidId());
                    }
                }
            }

            @Override
            public void onFailure(String response) {
                showMessage("获取室内机系列失败，请重试！");
            }
        });
    }



    /**
     *  上传房间信息数据到服务器
     */
    private void setRoomData() {
        Map<String, Object> param = new HashMap<>();
        param.put("roomData", mRoom);

        BasicHttpClient.httpPostAsync(HttpConstant.HTTP_POST_PROJECT_ROOM, AccountManager.getInstance().getToken(), param, new HttpCallbackHandle<GetRoomDataResp>() {
            @Override
            public void onSuccess(GetRoomDataResp obj) {
                // 成功

            }

            @Override
            public void onFailure(String response) {
                showMessage("上传房间信息数据失败，请重试！");
            }
        });

    }


    /**
     *  修改室内设备
     *
     *  @param tag     类型
     *  @param device 数据
     */
    private void modifyRoomDevice(String tag, IndoorEquipData device) {
        Map<String, Object> param = new HashMap<>();
        param.put("indoorEquip", device);

        BasicHttpClient.httpPostAsync(HttpConstant.HTTP_POST_ROOM_MACHINE, AccountManager.getInstance().getToken(), param, new HttpCallbackHandle<GetIndoorEquipDataResp>() {
            @Override
            public void onSuccess(GetIndoorEquipDataResp obj) {
                // 成功

            }

            @Override
            public void onFailure(String response) {
                showMessage("上传房间信息数据失败，请重试！");
            }
        });

    }

    /***
     * 添加一台室内机设备
     * @param device
     */
    private void addSnjRoomDevice(GuidData device) {
        for(IndoorEquipData item : mSnjSelectedDevice) {
            if (StringUtils.isEquals(device.getGuidId(), item.getEquipId())) {
                //已经添加了，不要再重复添加
                return;
            }
        }
        final IndoorEquipData data = new IndoorEquipData();
        data.setEquipId(device.getGuidId());
        data.setTag(ProjectInfoData.MACHINE_TAG_FOR_SNJ);
        data.setCount(1);
        data.setXh(device.getName());
        Map<String, Object> param = new HashMap<>();
        param.put("indoorEquip", data);

        String url = HttpConstant.HTTP_PUT_ROOM_MACHINE
                + "?tk=" + AccountManager.getInstance().getToken()
                + "&rid=" + mRoom.getGuidId();

        BasicHttpClient.httpPutAsync(url, param, new HttpCallbackHandle<GetIndoorEquipDataResp>() {
            @Override
            public void onSuccess(GetIndoorEquipDataResp obj) {
                // 成功
                data.setGuidId(obj.getCreateIndoorEquipResult());
                mSnjSelectedDevice.add(data);
                setSnjDeviceView();
            }

            @Override
            public void onFailure(String response) {
                showMessage("添加设备失败，请重试！");
            }
        });

    }

    /***
     * 添加一台轻型商用空调设备
     * @param device
     */
    private void addQxsyktRoomDevice(GuidData device) {
        for(IndoorEquipData item : mQxsyktSelectedDevice) {
            if (StringUtils.isEquals(device.getGuidId(), item.getEquipId())) {
                //已经添加了，不要再重复添加
                return;
            }
        }
        final IndoorEquipData data = new IndoorEquipData();
        data.setEquipId(device.getGuidId());
        data.setTag(ProjectInfoData.MACHINE_TAG_FOR_QXSYKT);
        data.setCount(1);
        data.setXh(device.getName());
        Map<String, Object> param = new HashMap<>();
        param.put("indoorEquip", data);

        String url = HttpConstant.HTTP_PUT_ROOM_MACHINE
                + "?tk=" + AccountManager.getInstance().getToken()
                + "&rid=" + mRoom.getGuidId();

        BasicHttpClient.httpPutAsync(url, param, new HttpCallbackHandle<GetIndoorEquipDataResp>() {
            @Override
            public void onSuccess(GetIndoorEquipDataResp obj) {
                // 成功
                data.setGuidId(obj.getCreateIndoorEquipResult());
                mQxsyktSelectedDevice.add(data);
                setQxsyktDeviceView();
            }

            @Override
            public void onFailure(String response) {
                showMessage("添加设备失败，请重试！");
            }
        });

    }


    /**
     *  获取用户选择的所有室内设备
     *
     *  @param tag <#tag description#>
     */
    private void loadRoomDevice(final String tag) {
        Map<String, Object> param = new HashMap<>();
        param.put("rid", mRoom.getGuidId());
        param.put("tag", tag);

        BasicHttpClient.httpGetAsync(HttpConstant.HTTP_GET_ROOM_ALL_MACHINE, AccountManager.getInstance().getToken(), param, new HttpCallbackHandle<GetIndoorEquipDataResp>() {
            @Override
            public void onSuccess(GetIndoorEquipDataResp obj) {
                // 成功
                loadFinishedCount++;
                if (loadFinishedCount == loadTotalCounts) {
                    mProgressDlg.dismiss();
                }
                if (StringUtils.isEquals(tag, ProjectInfoData.MACHINE_TAG_FOR_SNJ)) {
                    mSnjSelectedDevice.clear();
                    if (obj.getGetIndoorEquipsResult() != null && obj.getGetIndoorEquipsResult().size() > 0) {
                        mSnjSelectedDevice.addAll(obj.getGetIndoorEquipsResult());
                    }
                    setSnjDeviceView();
                } else if (StringUtils.isEquals(tag, ProjectInfoData.MACHINE_TAG_FOR_QXSYKT)) {
                    mQxsyktSelectedDevice.clear();
                    if (obj.getGetIndoorEquipsResult() != null && obj.getGetIndoorEquipsResult().size() > 0) {
                        mQxsyktSelectedDevice.addAll(obj.getGetIndoorEquipsResult());
                    }
                    setQxsyktDeviceView();
                }
            }

            @Override
            public void onFailure(String response) {
                loadFinishedCount++;
                if (loadFinishedCount == loadTotalCounts) {
                    mProgressDlg.dismiss();
                }
                showMessage("获取用户选择的所有室内设备失败，请重试！");
            }
        });
    }

    /***
     * 对室内机自动选型
     */
    private void getSNJDefaultDevice() {
        if (StringUtils.isEmpty(ProjectManager.getInstance().getProjectDefaultSeries(ProjectInfoData.MACHINE_TAG_FOR_SNJ))) {
            IOSAlertView.showAlertView(RoomInfoActivity.this, "提示", "超出该系列能力范围，请选择其他合适型式。","确认", null);
            return;
        }

        mProgressDlg = createProgressBarDialog(RoomInfoActivity.this, "自动选型中...");
        mProgressDlg.show();
        Map<String, Object> param = new HashMap<>();
        param.put("rid", mRoom.getGuidId());
        param.put("xsid", ProjectManager.getInstance().getProjectDefaultSeries(ProjectInfoData.MACHINE_TAG_FOR_SNJ));

        BasicHttpClient.httpGetAsync(HttpConstant.HTTP_GET_SNJ_DEFAULT_DEVICE, AccountManager.getInstance().getToken(), param, new HttpCallbackHandle<GetIndoorEquipDataResp>() {
            @Override
            public void onSuccess(GetIndoorEquipDataResp obj) {
                // 成功
                mProgressDlg.dismiss();
                if (obj.getAutoSnjEquipResult() != null) {
                    mSnjSelectedDevice.clear();
                    mSnjSelectedDevice.add(obj.getAutoSnjEquipResult());
                    setSnjDeviceView();
                }
                else {
                    IOSAlertView.showAlertView(RoomInfoActivity.this, "提示", "超出该系列能力范围，请选择其他合适型式。","确认", null);
                }
            }

            @Override
            public void onFailure(String response) {
                mProgressDlg.dismiss();
                IOSAlertView.showAlertView(RoomInfoActivity.this, "提示", "超出该系列能力范围，请选择其他合适型式。", "确认", null);
            }
        });

    }

    /***
     * 对轻型商用空调自动选型
     */
    private void getQXSYKTDefaultDevice() {
        if (StringUtils.isEmpty(ProjectManager.getInstance().getProjectDefaultSeries(ProjectInfoData.MACHINE_TAG_FOR_QXSYKT))) {
            IOSAlertView.showAlertView(RoomInfoActivity.this, "提示", "没有获取到默认型式，自动选型失败！","确认", null);
            return;
        }

        mProgressDlg = createProgressBarDialog(RoomInfoActivity.this, "自动选型中...");
        mProgressDlg.show();
        Map<String, Object> param = new HashMap<>();
        param.put("rid", mRoom.getGuidId());
        param.put("xsid", ProjectManager.getInstance().getProjectDefaultSeries(ProjectInfoData.MACHINE_TAG_FOR_QXSYKT));

        BasicHttpClient.httpGetAsync(HttpConstant.HTTP_GET_QXSYKT_DEFAULT_DEVICE, AccountManager.getInstance().getToken(), param, new HttpCallbackHandle<GetIndoorEquipDataResp>() {
            @Override
            public void onSuccess(GetIndoorEquipDataResp obj) {
                // 成功
                mProgressDlg.dismiss();
                if (obj.getAutoQxSyKtEquipResult() != null) {
                    mQxsyktSelectedDevice.clear();
                    mQxsyktSelectedDevice.add(obj.getAutoQxSyKtEquipResult());
                    setQxsyktDeviceView();
                }
                else {
                    IOSAlertView.showAlertView(RoomInfoActivity.this, "提示", "超出该系列能力范围，请选择其他合适型式。","确认", null);
                }
            }

            @Override
            public void onFailure(String response) {
                mProgressDlg.dismiss();
                IOSAlertView.showAlertView(RoomInfoActivity.this, "提示", "超出该系列能力范围，请选择其他合适型式。", "确认", null);
            }
        });

    }

    private void setSnjDeviceView() {
        if (mSnjSelectedDevice.size() > 0) {
            mSnjContentLl.setVisibility(View.VISIBLE);
            mSnjContentLl.removeAllViews();
            for (final IndoorEquipData item : mSnjSelectedDevice) {
                CellSeperateLineView line = new CellSeperateLineView(this);
                mSnjContentLl.addView(line);

                final DeviceItemView view = new DeviceItemView(this);
                view.setDeviceString(item.getXh());
                view.setCount(item.getCount());

                view.setOnMinusClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int count = view.getCount();
                        if (count > 1) {
                            count--;
                        } else {
                            return;
                        }
                        item.setCount(count);
                        view.setCount(count);
                        modifyRoomDevice(ProjectInfoData.MACHINE_TAG_FOR_SNJ, item);
                    }
                });

                view.setOnAddClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int count = view.getCount();
                        count++;
                        item.setCount(count);
                        view.setCount(count);
                        modifyRoomDevice(ProjectInfoData.MACHINE_TAG_FOR_SNJ, item);
                    }
                });

                view.setOnDeleteClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //删除设备

                        Map<String, Object> param = new HashMap<>();
                        param.put("tag", ProjectInfoData.MACHINE_TAG_FOR_SNJ);
                        param.put("ieid", item.getGuidId());
                        mProgressDlg = createProgressBarDialog(RoomInfoActivity.this, "删除设备中...");
                        mProgressDlg.show();

                        BasicHttpClient.httpDeleteAsync(HttpConstant.HTTP_DELETE_ROOM_MACHINE, AccountManager.getInstance().getToken(), param, new HttpCallbackHandle<GetIndoorEquipDataResp>() {
                            @Override
                            public void onSuccess(GetIndoorEquipDataResp obj) {
                                // 成功
                                mProgressDlg.dismiss();
                                mSnjSelectedDevice.remove(item);
                                mSnjContentLl.removeView(view);
                            }

                            @Override
                            public void onFailure(String response) {
                                mProgressDlg.dismiss();
                                showMessage("上传房间信息数据失败，请重试！");
                            }
                        });
                    }
                });
                mSnjContentLl.addView(view);
            }
        }
        else {
            mSnjContentLl.setVisibility(View.GONE);
        }
    }

    private void setQxsyktDeviceView() {
        if (mQxsyktSelectedDevice.size() > 0) {
            mQxsyktContentLl.setVisibility(View.VISIBLE);
            mQxsyktContentLl.removeAllViews();
            for (final IndoorEquipData item : mQxsyktSelectedDevice) {
                CellSeperateLineView line = new CellSeperateLineView(this);
                mQxsyktContentLl.addView(line);

                final DeviceItemView view = new DeviceItemView(this);
                view.setDeviceString(item.getXh());
                view.setCount(item.getCount());

                view.setOnMinusClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int count = view.getCount();
                        if (count > 1) {
                            count--;
                        } else {
                            return;
                        }
                        item.setCount(count);
                        view.setCount(count);
                        modifyRoomDevice(ProjectInfoData.MACHINE_TAG_FOR_QXSYKT, item);
                    }
                });

                view.setOnAddClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int count = view.getCount();
                        count++;
                        item.setCount(count);
                        view.setCount(count);
                        modifyRoomDevice(ProjectInfoData.MACHINE_TAG_FOR_QXSYKT, item);
                    }
                });

                view.setOnDeleteClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //删除设备
                        Map<String, Object> param = new HashMap<>();
                        param.put("tag", ProjectInfoData.MACHINE_TAG_FOR_QXSYKT);
                        param.put("ieid", item.getGuidId());
                        mProgressDlg = createProgressBarDialog(RoomInfoActivity.this, "删除设备中...");
                        mProgressDlg.show();

                        BasicHttpClient.httpDeleteAsync(HttpConstant.HTTP_DELETE_ROOM_MACHINE, AccountManager.getInstance().getToken(), param, new HttpCallbackHandle<GetIndoorEquipDataResp>() {
                            @Override
                            public void onSuccess(GetIndoorEquipDataResp obj) {
                                // 成功
                                mProgressDlg.dismiss();
                                mQxsyktSelectedDevice.remove(item);
                                mQxsyktContentLl.removeView(view);
                            }

                            @Override
                            public void onFailure(String response) {
                                mProgressDlg.dismiss();
                                showMessage("上传房间信息数据失败，请重试！");
                            }
                        });

                    }
                });
                mQxsyktContentLl.addView(view);
            }
        }
        else {
            mQxsyktContentLl.setVisibility(View.GONE);
        }
    }

    /**
     * 回调
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != this.RESULT_OK) {
            return;
        }
        if (requestCode == Select_SNJ) {
            if (data != null) {
                mSnjDevice = data.getParcelableArrayListExtra("device");
                if (mSnjDevice != null && mSnjDevice.size() > 0) {
                    for (GuidData item: mSnjDevice) {
                        addSnjRoomDevice(item);
                    }
                }
            }
        } else if (requestCode == Select_QXSYKT) {
            if (data != null) {
                mQxsyktDevice = data.getParcelableArrayListExtra("device");
                if (mQxsyktDevice != null && mQxsyktDevice.size() > 0) {
                    for (GuidData item: mQxsyktDevice) {
                        addQxsyktRoomDevice(item);
                    }
                }
            }
        } else if (requestCode == Select_SNJ_XS) {
            if (data != null) {
                List<GuidData> xs = data.getParcelableArrayListExtra("device");
                if (xs != null && xs.size() > 0) {
                    //开始自动选型
                    ProjectManager.getInstance().setProjectDefaultSeries(ProjectInfoData.MACHINE_TAG_FOR_SNJ, xs.get(0).getGuidId());
                    getSNJDefaultDevice();
                }
            }
        } else if (requestCode == Select_QXSYKT_XS) {
            if (data != null) {
                List<GuidData> xs = data.getParcelableArrayListExtra("device");
                if (xs != null && xs.size() > 0) {
                    //开始自动选型
                    ProjectManager.getInstance().setProjectDefaultSeries(ProjectInfoData.MACHINE_TAG_FOR_QXSYKT, xs.get(0).getGuidId());
                    getQXSYKTDefaultDevice();
                }
            }
        }

    }

}
